package com.kly.test.init_model
import android.app.Application
import com.framework.network.callbacks.OnAddHeadDynamicParamsListener
import com.framework.network.callbacks.OnNetworkResultListener
import com.framework.network.manager.NetworkManager
import com.framework.network.remote.LoginInvalidApiException
import com.kly.module_base.base.BaseInitModel
import com.kly.module_base.utils.LogUtil
import com.kly.module_base.utils.SaveDeviceUtil
import com.kly.test.BuildConfig
import com.kly.test.ProcessObserver
import com.kly.test.ToastUtil
import com.kly.test.api.ApiService
import com.kly.test.api.NetworkParams
import com.kly.test.api.RootDomainManager
import okhttp3.Request

class InitNetworkModel : BaseInitModel() {
    override fun init(application: Application) {
        //先取缓存中的数据
        var baseUrl = SaveDeviceUtil.getRootDomain()
        //若为空再取默认数据
        if (baseUrl.isBlank()) {
            baseUrl = BuildConfig.BASE_URl
        }
        //若还是为空，提示异常
        if (baseUrl.isBlank()) {
            ToastUtil.showToast("网络请求domain未设置")
            return
        }
        NetworkManager.getInstance().create()
            .addBaseUrl(baseUrl)
            .isOpenLog(BuildConfig.DEBUG)
            .addClassType(ApiService::class.java)
            .addCommonParams(NetworkParams.getCommonParams())
            .addDomainToMap(RootDomainManager.getInstance(application).getDomainMap(application))
            .addHeadParams(NetworkParams.getHeadCommonParams())
            .setOnAddHeadDynamicParamsListener(object : OnAddHeadDynamicParamsListener() {
                override fun onHeadDynamicParams(request: Request): HashMap<String, Any> {
                    return NetworkParams.getMapWithHeaders(request)
                }
            })
            .setOnNetworkResultListener(object : OnNetworkResultListener() {
                override fun onNetworkBreak(e: Exception) {
                    super.onNetworkBreak(e)
                    LogUtil.errorLog("您的网络已断开，请检查网络连接")
                }

                override fun onSocketTimeout(e: Exception) {
                    super.onSocketTimeout(e)
                   LogUtil.errorLog("请求超时，请稍后再试")
                }

                override fun onJsonSyntax(e: Exception) {
                    super.onJsonSyntax(e)
                   LogUtil.errorLog("数据解析错误")
                }

                override fun onServiceCallBackError(e: Exception) {
                    super.onServiceCallBackError(e)
                    (if (e.message.isNullOrBlank()) {
                        "服务器繁忙，请稍后再试"
                    } else {
                        e.message
                    })?.let {
                       LogUtil.errorLog(
                            it
                        )
                    }
                }

                override fun onThrowableError(e: Throwable) {
                    super.onThrowableError(e)
                    //登录失效
                    if (e is LoginInvalidApiException) {
                        ProcessObserver.currentActivity?.let {
//                            CustomPushManager.getInstance().closePush(SaveDeviceUtil.getUserID())
                            SaveDeviceUtil.clearUserInf()
                           LogUtil.errorLog(e.msg ?: "登录已失效，请重新登录")
//                            MobileLoginActivity.startActivity(it)
                        }
                    } else {
//                       LogUtil.errorLog(
//                            if (BuildConfig.DEBUG) {
//                                e.message ?: "服务器繁忙，请稍后再试"
//                            } else {
//                                "服务器繁忙，请稍后再试"
//                            }
//                        )
                    }
                }
            })
            .build()
            .init(application)
    }
}